import random
from datetime import date

from django.http import JsonResponse
from django.shortcuts import render, HttpResponse

# Create your views here.
from django.utils import timezone

from personal.models import Personal, Question


def index(request):
    content = request.session.get('personal')

    if not content:
        return render(request, 'Initial/login.html')

    return render(request, 'Initial/index.html', content)


def login(request):
    request.session.clear()

    return render(request, 'Initial/login.html')


def edit(request):
    content = request.session.get('personal')

    if not content:
        return render(request, 'Initial/login.html')

    return render(request, 'Initial/edit.html', content)


def dologin(request):  # 判断是否为一个合法用户

    username = request.POST.get('name')
    password = request.POST.get('password')

    # try:
    #   isleguser = Personal.objects.get(user_account=username)
    # except Exception as e:
    #   return render(request, 'Initial/login.html')
    # else:
    #    return render(request, 'Initial/index.html')

    isleguser = Personal.objects.filter(user_account=username, user_password=password)

    if isleguser:

        content = {
            'user_id': isleguser[0].user_id,
            'user_account': isleguser[0].user_account,
            'user_name': isleguser[0].user_name,
            'user_lable': isleguser[0].user_lable,
            'user_gender': isleguser[0].user_gender,
            'user_address_province': isleguser[0].user_address_province,
            'user_address_city': isleguser[0].user_address_city,
            'user_industry': isleguser[0].user_industry,
            'user_profession': isleguser[0].user_profession,
            'user_email': isleguser[0].user_email,
            'user_education': isleguser[0].user_education,
            'user_birthday': str(isleguser[0].user_birthday),
            'user_phone': isleguser[0].user_phone,
            'user_introduce': isleguser[0].user_introduce,
            'user_photo': isleguser[0].user_photo.url,
        }

        request.session['personal'] = content  # 将合法用户存入session  只能传字符串，整数，this

        return render(request, 'Initial/index.html', content)
    else:
        return render(request, 'Initial/login.html')


def uploadphoto(request):  # 从session中取出该用户的信息，取出id，根据id在数据库中找到记录，再添加文件路径

    s = request.session.get('personal')

    if not s:
        return render(request, 'Initial/login.html')

    uid = s['user_id']

    p = Personal.objects.get(user_id=uid)

    p.user_photo = request.FILES.get('photo')
    # 将上传的文件保存到DB
    p.save()

    # 将上传的文件更新到session
    s['user_photo'] = p.user_photo.url

    request.session['personal'] = s

    content = {
        'user_photo': s['user_photo']
    }

    ret = JsonResponse(content)
    return HttpResponse(ret)


def topic(request):
    content = request.session.get('personal')

    questions = Question.objects.all()

    content['questions'] = questions

    return render(request, 'Initial/topic.html', content)


def dohyh(request):
    list = ['军事', '游戏', '音乐', '国内', '国际', '财经', '娱乐', '体育', '互联网', '科技', '游戏', '女人', '汽车', '房产']

    i = random.randint(0, len(list))
    content = {
        'title1': list[i % len(list)],
        'title2': list[(i + 1) % len(list)],
        'title3': list[(i + 2) % len(list)]
    }
    ret = JsonResponse(content)
    return HttpResponse(ret)


def updateInfo(request):
    s = request.session.get('personal')

    if not s:
        return render(request, 'Initial/login.html')

    uid = s['user_id']

    p = Personal.objects.get(user_id=uid)

    gender = request.POST.get('sex')
    if gender == 'male':
        p.user_gender = True
    else:
        p.user_gender = False

    p.user_address_province = request.POST.get('province')
    p.user_address_city = request.POST.get('city')
    p.user_industry = request.POST.get('job')
    p.user_email = request.POST.get('email')
    p.user_education = request.POST.get('education')
    p.user_birthday = request.POST.get('birthday')
    p.user_phone = request.POST.get('phone')
    p.user_introduce = request.POST.get('introduce')

    p.save()

    s['user_gender'] = p.user_gender
    s['user_address_province'] = p.user_address_province
    s['user_address_city'] = p.user_address_city
    s['user_industry'] = p.user_industry
    s['user_email'] = p.user_email
    s['user_education'] = p.user_education
    s['user_birthday'] = p.user_birthday
    s['user_phone'] = p.user_phone
    s['user_introduce'] = p.user_introduce

    request.session['personal'] = s

    return HttpResponse('ok')


def doquestions(request):
    content = request.session.get('personal')

    p = Question()

    uesr = content['user_account']
    title = request.POST.get('title')
    huati = request.POST.get('huati')
    miaoshu = request.POST.get('miaoshu')
    niming = True

    p.q_title = title
    p.q_content = miaoshu
    p.q_label = huati
    p.q_create_time = timezone.now()
    p.q_anonymous = niming
    p.q_user = uesr

    p.save()

    questions = Question.objects.all()

    content['questions'] = questions

    return render(request, 'Initial/topic.html', content)


def add_zan(request):
    title = request.POST.get('title')

    z = Question.objects.get(q_title=title)

    z.q_zan = z.q_zan + 1

    z.save()

    content = {
        'zan': z.q_zan
    }
    ret = JsonResponse(content)

    return HttpResponse(ret)


def dpquestion(request):
    s = request.session.get('personal')

    user = s['user_account']

    p_question = Question.objects.filter(q_user=user)

    content = {
        'p_question': p_question
    }

    return render(request, 'Initial/index.html', content)
